package com.qk.manangement.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qk.common.response.PageResult;
import com.qk.entity.pojo.Role;
import com.qk.entity.request.role.AddRoleRequest;
import com.qk.manangement.service.RoleService;
import com.qk.manangement.mapper.RoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 *
 */
@Service
public class RoleServiceImpl implements RoleService{
    @Autowired
    private RoleMapper roleMapper;

    @Override
    public PageResult<Role> getRoleList(String name, String label, Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);
        List<Role> roleList = roleMapper.getRoleList(name, label);
        PageInfo<Role> rolePageInfo = new PageInfo<>(roleList);
        return new PageResult<>(rolePageInfo.getTotal(), rolePageInfo.getList());
    }

    @Override
    public boolean deleteRole(Integer id) {
        return roleMapper.deleteRole(id) > 0;
    }

    @Override
    public boolean addRole(AddRoleRequest addRoleRequest) {
        Role role = new Role();
        role.setName(addRoleRequest.getName());
        role.setLabel(addRoleRequest.getLabel());
        role.setRemark(addRoleRequest.getRemark());
        role.setCreateTime(LocalDateTime.now());
        role.setUpdateTime(LocalDateTime.now());
        return roleMapper.addRole(role) > 0;
    }

    @Override
    public Role getById(Integer id) {
        Role role = roleMapper.getById(id);
        if (role != null) {
            return role;
        }
        return null;
    }

    @Override
    public boolean updateRole(Role role) {
        role.setUpdateTime(LocalDateTime.now());
        return roleMapper.updateRole(role) > 0;
    }

    @Override
    public List<Role> list() {
        List<Role> roleList = roleMapper.getRoleList( null, null);
        return roleList;
    }
}




